//
//  CircleProgressView.m
//  ProgressView
//
//  Created by zhao on 16/9/13.
//  Copyright © 2016年 zhaoName. All rights reserved.
//  正常的圆形进度条

#import "CircleProgressView.h"

#define WIDTH self.frame.size.width
#define HEIGHT self.frame.size.height

@implementation CircleProgressView

- (instancetype)initWithFrame:(CGRect)frame
{
    if([super initWithFrame:frame])
    {
        [self initData];
    }
    return self;
}

- (instancetype)init
{
    if([super init])
    {
        [self initData];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    if([super initWithCoder:aDecoder])
    {
        [self initData];
    }
    return self;
}

/** 初始化数据*/
- (void)initData
{
    self.progressWidth = 14.0;
    //self.progressColor = [UIColor colorWithRed:42/255.00 green:141/255.00 blue:228/255.00 alpha:1];
    //self.progressBackgroundColor = [UIColor colorWithRed:230/255.00 green:230/255.00 blue:230/255.00 alpha:1];
    self.progressColor = [UIColor colorWithRed:255/255.00 green:165/255.00 blue:79/255.00 alpha:1];
    self.progressBackgroundColor = [UIColor whiteColor];
    self.percent = 0.0;
    self.clockwise = 0;
    
    self.labelbackgroundColor = [UIColor clearColor];
    self.textColor = [UIColor blackColor];
    self.textFont = [UIFont systemFontOfSize:15];
    
    _topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (HEIGHT-30)/2-25, WIDTH, 30)];
    _topLabel.textAlignment = NSTextAlignmentCenter;
    _topLabel.font = [UIFont systemFontOfSize:16];
    //_topLabel.textColor = [UIColor darkGrayColor];
    _topLabel.textColor = [UIColor whiteColor];
    _topLabel.text = @"已完成占比";
    [self addSubview:self.topLabel];
    
    _centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (HEIGHT-30)/2, WIDTH, 30)];
    _centerLabel.textAlignment = NSTextAlignmentCenter;
    _centerLabel.font = [UIFont systemFontOfSize:22];
    //_centerLabel.textColor = [UIColor darkGrayColor];
    _centerLabel.textColor = [UIColor whiteColor];
    [self addSubview:self.centerLabel];
    
    _bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (HEIGHT-30)/2+25, WIDTH, 30)];
    _bottomLabel.textAlignment = NSTextAlignmentCenter;
    _bottomLabel.font = [UIFont systemFontOfSize:11];
    //_bottomLabel.textColor = [UIColor darkGrayColor];
    _bottomLabel.textColor = [UIColor whiteColor];
    _bottomLabel.text = @"剩0.0m³";
    [self addSubview:self.bottomLabel];
}

- (void)layoutSubviews
{
//    _topLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (HEIGHT-30)/2-40, WIDTH, 30)];
//    _topLabel.textAlignment = NSTextAlignmentCenter;
//    _topLabel.font = [UIFont systemFontOfSize:18];
//    _topLabel.text = @"已完成占比";
//    [self addSubview:self.topLabel];
//    
//    _centerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (HEIGHT-30)/2, WIDTH, 30)];
//    _centerLabel.textAlignment = NSTextAlignmentCenter;
//    _centerLabel.font = [UIFont systemFontOfSize:22];
//    [self addSubview:self.centerLabel];
//    
//    _bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, (HEIGHT-30)/2+40, WIDTH, 30)];
//    _bottomLabel.textAlignment = NSTextAlignmentCenter;
//    _bottomLabel.font = [UIFont systemFontOfSize:12];
//    _bottomLabel.text = @"剩2000.0m³";
//    [self addSubview:self.bottomLabel];
    
//    self.centerLabel.backgroundColor = self.labelbackgroundColor;
//    self.centerLabel.textColor = self.textColor;
//    self.centerLabel.font = self.textFont;
//    [self addSubview:self.centerLabel];
}

#pragma mark -- 画进度条

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetShouldAntialias(context, YES); // 平滑
    CGContextAddArc(context, WIDTH/2, HEIGHT/2, (WIDTH-self.progressWidth)/2, 0, M_PI*2, 0);
    [self.progressBackgroundColor setStroke];
    CGContextSetLineWidth(context, self.progressWidth);
    CGContextStrokePath(context);
    
    if(self.percent)
    {
        CGFloat angle = 2 * self.percent * M_PI - M_PI_2;
        if(self.clockwise) {// 反方向
            CGContextAddArc(context, WIDTH/2, HEIGHT/2, (WIDTH-self.progressWidth)/2, ((int)self.percent == 1 ? -M_PI_2 : angle), -M_PI_2, 0);
        } else {// 正方向
            CGContextAddArc(context, WIDTH/2, HEIGHT/2, (WIDTH-self.progressWidth)/2, -M_PI_2, angle, 0);
        }
        [self.progressColor setStroke];
        CGContextSetLineWidth(context, self.progressWidth);
        CGContextStrokePath(context);
    }
}

- (void)setPercent:(float)percent
{
    if(self.percent < 0) return;
    _percent = percent;
    
    [self setNeedsDisplay];
}

@end
